﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using AppData;
using AppData.DTO;
using Microsoft.AspNetCore.Components;

namespace EbookReader
{
    public  class RequestController
    {
        static RequestController _instance;
        public static RequestController instance
        {
            get
            {
                if (_instance == null)
                    _instance = new RequestController();

                return _instance;
            }
        }

        public static string serverHost = "127.0.0.1";//"192.168.31.144";"172.21.192.1"
        private static int port = 7014;//7014  5202
        static string appServerUrl => $"https://{serverHost}:{port}";
        //static string appServerUrl => $"https://{serverHost}:{port}/api";
        static string serverBooksUrl => appServerUrl+"/Books";

        public static string get_recommended_books_Url = serverBooksUrl + "/get_recommended_books";
        public static string get_books_with_name_Url = serverBooksUrl + "/get_books_with_name";
        public static string upload_book_Url = serverBooksUrl + "/upload_book";
        public static string upload_book_with_str_Url = serverBooksUrl + "/upload_book_with_str";

        public async Task<List<Book>> GetReommendedBooks(int num,Action<string> onError)//Action<List<Book>> callback)
        {
            Dictionary<string, object> param = new Dictionary<string, object>()
            {
                { "num", num }
            };

            //AsyncHttpRequest httpRequest = new AsyncHttpRequest(get_recommended_books_Url, param);

            //GameDebug.Log($"GetReommendedBooks url:{httpRequest.webRequest.RequestUri}");

            //httpRequest.onResponse = (request) =>
            //{
            //    GameDebug.Log($"GetReommendedBooks  response:{request?.error}");

            //    if (string.IsNullOrEmpty(request.error))
            //    {
            //        if (!string.IsNullOrEmpty(request.response))
            //        {
            //            var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Book>>(request.response);

            //            GameDebug.Log($"get books:{list?.Count}");
            //            callback?.Invoke(list);
            //        }
            //        else
            //        {
            //            GameDebug.LogError($"无返回数据");
            //            callback?.Invoke(null);
            //        }
            //    }
            //    else
            //    {
            //        GameDebug.LogError($"请求失败：{request.error}");
            //        callback?.Invoke(null);
            //    }

            //};

            //httpRequest.GET();

            string response = await WebUtility.HttpGet(get_recommended_books_Url, param, (err) =>
            {
                GameDebug.LogError($"请求失败：{err}");
                onError?.Invoke(err);
            });

            if (!string.IsNullOrEmpty(response))
            {
                var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Book>>(response);

                GameDebug.Log($"get books:{list?.Count}");
                return list;
            }
            else
            {
                GameDebug.LogError($"无返回数据");
                return null;
            }

        }
        public void GetBooksWithName(string name, Action<List<Book>> callback)
        {
            Dictionary<string, object> param = new Dictionary<string, object>()
            {
                { "name", name }
            };

            AsyncHttpRequest httpRequest = new AsyncHttpRequest(get_recommended_books_Url, param);

            GameDebug.Log($"GetReommendedBooks url:{httpRequest.webRequest.RequestUri}");

            httpRequest.onResponse = (request) =>
            {
                GameDebug.Log($"GetBooksWithName  response:{request?.error}");

                if (string.IsNullOrEmpty(request.error))
                {
                    if (!string.IsNullOrEmpty(request.response))
                    {
                        var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Book>>(request.response);

                        GameDebug.Log($"get books:{list?.Count}");
                        callback?.Invoke(list);
                    }
                    else
                    {
                        GameDebug.LogError($"无返回数据");
                        callback?.Invoke(null);
                    }
                }
                else
                {
                    GameDebug.LogError($"请求失败：{request.error}");
                    callback?.Invoke(null);
                }

            };

            httpRequest.GET();
        }

        public void UploadBook(BookProto book,Action<Book> callback)
        {
            Dictionary<string, object> param = HttpRequestForm.GetProtoDict(book);

            AsyncHttpRequest httpRequest = new AsyncHttpRequest(upload_book_Url);

            httpRequest.onResponse = (request) =>
            {
                if (string.IsNullOrEmpty(request.error))
                {
                    GameDebug.Log($"upload book suc:{book?.name}");

                    Book bookObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Book>(request.response);
                    callback?.Invoke(bookObj);
                }
                else
                {
                    GameDebug.LogError($"请求失败：{request.error}");
                    callback?.Invoke(null);
                }
            };
            httpRequest.POSTForm(param);

            //WebUtility.HttpPost(upload_book_Url, book, (code, response) =>
            //{
            //    if (!string.IsNullOrEmpty(response))
            //    {
            //        GameDebug.Log($"upload book suc:{book?.name}");

            //        Book bookObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Book>(response);
            //        callback?.Invoke(bookObj);
            //    }
            //    else
            //    {
            //        GameDebug.LogError($"无返回数据");
            //        callback?.Invoke(null);
            //    }
            //}, (err) =>
            //{
            //    GameDebug.LogError($"请求失败：{err}");
            //    callback?.Invoke(null);
            //});

        }
    }
}
